package com.chb.service.impl;

import com.chb.dao.BranchDao;
import com.chb.model.entity.Branch;
import com.chb.model.entity.BranchGoods;
import com.chb.model.entity.Role;
import com.chb.model.entity.Staff;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.BranchService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author ：张家琪
 * @date ：Created in 2023/9/26 22:04
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchDao branchDao;
    @Autowired
    private HttpSession session;

    @Override
    public Result selectAll() {
        Staff staff = (Staff) session.getAttribute("staff");
        List<Branch> list = branchDao.findAll(staff.getStaffid());
        return new Result(200, "查询成功", list);
    }


    /**
     * @create by: 陈海彬
     * @description: 查询所有分店信息
     * @create time: 2023/9/26 17:50
     */
    @Override
    public Result findAllBranch() {
        //1.获取用户的信息
        Staff staff = (Staff) session.getAttribute("staff");
        //2.进行查询部门下拉框信息
        List<Branch> allBranch = branchDao.findAllBranch(staff.getBranchid(), staff.getStaffid());
        return Result.success(allBranch);
    }

    /**
     * @param
     * @param branchname
     * @return
     * @create by: xIAO_ZHOU
     * @description: 查询所有店铺信息（店长、开店时间...）以及该店员工人数
     * @create time: 2023/9/30 22:16
     */
    @Override
    public PageBean<Branch> selectAllBranchAndStaff(Integer currentPage, Integer pageSize, String branchname) {
        //分页插件
        Staff staff = (Staff) session.getAttribute("staff");
        PageHelper.startPage(currentPage, pageSize);
        Branch branch = new Branch();
        branch.setStaffid(staff.getStaffid());
        branch.setBranchid(staff.getBranchid());
        if (branchname != null) {
            branch.setBranchname(branchname);
        }
        List<Branch> branches = branchDao.selectAllBranchAndStaff(branch);
        PageInfo<Branch> pageInfo = new PageInfo<>(branches);
        return new PageBean<Branch>(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 根据店铺名称模糊查询店铺信息
     * @create time: 2023/10/4 20:51
     */
    @Override
    public Result queryBranchInfoBybranchname(Branch branchname) {
        Staff staff = (Staff) session.getAttribute("staff");
        branchname.setBranchid(staff.getBranchid());
        branchname.setStaffid(staff.getStaffid());
        List<Branch> branches = branchDao.queryBranchInfoBybranchname(branchname);
        return new Result(200, "查询成功", branches);

    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 添加新的店铺及信息
     * @create time: 2023/10/5 20:09
     */
    @Override
    public Integer insertNewBranch(Branch branch) {
        Integer integer = branchDao.insertNewBranch(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 通过店铺id 进行修改店铺信息
     * @create time: 2023/10/5 20:38
     */
    @Override
    public Integer updateBranch(Branch branch) {
        Integer integer = branchDao.updateBranch(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 通过店铺id 进行修改店铺状态（营业/整顿）
     * @create time: 2023/10/8 11:31
     */
    @Override
    public Integer adjustBranch(Branch branch) {
        Integer integer = branchDao.adjustBranch(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 通过店铺id 删除店铺信息
     * @create time: 2023/10/8 15:11
     */

    @Override
    public Integer delBranch(Branch branch) {
        Integer integer = branchDao.delBranch(branch);
        return integer;
    }
    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 查询所删除的店铺是否存在商品
     * @create time: 2023/10/13 11:11
     */

    @Override
    public Result queryProduct(Branch branch) {
        List<Branch> branchGoods = branchDao.queryProduct(branch);
        return new Result(200, "查询成功", branchGoods);
    }


    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 查询在职且没有安排岗位的员工（可以安排为店长的员工）
     * @create time: 2023/10/8 15:56
     */
    @Override
    public Result selectNoArrangementBranch() {
        List<Staff> staff = branchDao.selectNoArrangementBranch();
        return new Result(200, "查询成功", staff);
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 修改员工所在的店铺（修改staff 的 branchid）【确定员工为新店的店长后，将该员工之前的branchid 修改为新店的branchid】
     * @create time: 2023/10/8 20:25
     */
    @Override
    public Integer changeBranchid(Branch branch) {
        Integer integer = branchDao.changeBranchid(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 给店铺添加上店长的staffid
     * @create time: 2023/10/9 15:51
     */
    @Override
    public Integer updateStaffidforBranch(Branch branch) {
        Integer integer = branchDao.updateStaffidforBranch(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 确定员工为新店的店长后，将该员工之前的roleid 修改为店长的roleid
     * @create time: 2023/10/9 19:23
     */
    @Override
    public Integer changeRoleid(Branch branch) {
        Integer integer = branchDao.changeRoleid(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 根据员工的staffid 查询roleid 再查询rolename
     * @create time: 2023/10/9 19:45
     */
    @Override
    public Result selectRoleNameByStaffid(Branch branch) {
        List<Role> staff = branchDao.selectRoleNameByStaffid(branch);
        return new Result(200, "查询成功", staff);
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 加载店长调离框中所有职位信息
     * @create time: 2023/10/9 20:56
     */
    @Override
    public Result selectAllRoleName() {
        List<Role> staff = branchDao.selectAllRoleName();
        return new Result(200, "查询成功", staff);

    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 加载店长调离框中所有店名
     * @create time: 2023/10/9 20:57
     */

    @Override
    public Result selectAllBranchName() {
        List<Branch> staff = branchDao.selectAllBranchName();
        return new Result(200, "查询成功", staff);
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 调离后将原来店铺的店长staffid改为 null
     * @create time: 2023/10/10 11:24
     */
    @Override
    public Integer changeManagerStaffid(Branch branch) {
        Integer integer = branchDao.changeManagerStaffid(branch);
        return integer;
    }


    /**
     * @param
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取平调分店下拉框信息
     * @create time: 2023/10/3 13:48
     */
    @Override
    public Result flatInformation() {
        //1.获取用户的信息
        Staff staff = (Staff) session.getAttribute("staff");
        List<Branch> allBranch = branchDao.flatInformation(staff.getBranchid());
        return Result.success(allBranch);
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 调离后将原来店铺的店长branchid改为 新店的branchid
     * @create time: 2023/10/10 11:46
     */
    @Override
    public Integer changeManagerBranchid(Branch branch) {
        Integer integer = branchDao.changeManagerBranchid(branch);
        return integer;
    }

    /**
     * @param
     * @return
     * @create by: xIAO_ZHOU
     * @description: 调离后将原来店铺的店长deptid改为 新店的deptid
     * @create time: 2023/10/10 11:46
     */
    @Override
    public Integer changeManagerRoleid(Branch branch) {
        Integer integer = branchDao.changeManagerRoleid(branch);
        return integer;
    }
}